package com.bfxy.rabbitmq.api.exchange.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Sender4TopicExchange {

	public static void main(String[] args) throws Exception {

		Connection connection = null;
		Channel channel = null;
		try {
			// 1 创建ConnectionFactory
			ConnectionFactory connectionFactory = new ConnectionFactory();
			connectionFactory.setHost("192.168.218.21");
			connectionFactory.setPort(5672);
			connectionFactory.setVirtualHost("/");

			// 2 创建Connection
			connection = connectionFactory.newConnection();

			// 3 创建Channel
			channel = connection.createChannel();

			// 4 声明交换机
			String exchangeName = "test_topic_exchange";
			String exchangeType = "topic";
			channel.exchangeDeclare(exchangeName, exchangeType, true, false, false, null);

			// 5 消息发送
			String routingKey1 = "user.save";
			String routingKey2 = "user.update";
			String routingKey3 = "user.delete.abc";
			String msg = "Hello World RabbitMQ 4 Topic Exchange Message ...";
			channel.basicPublish(exchangeName, routingKey1 , null , msg.getBytes());
			channel.basicPublish(exchangeName, routingKey2 , null , msg.getBytes());
			channel.basicPublish(exchangeName, routingKey3 , null , msg.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (channel != null) {
				channel.close();
			}
			if (connection != null) {
				connection.close();
			}
		}

	}
	
}
